package itheima.d05;

import itheima.d05.exception.PasswordErrorException;

import javax.security.auth.login.AccountLockedException;
import javax.security.auth.login.AccountNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM   {
    private ArrayList<Account> accounts = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);
    private Account loginAcc;

    //启动ATM系统 展示欢迎界面
    public void start() throws Exception {
        Account account1 = new Account("110", "张三", '男', "23456", 5000.0, 2000.0);
        Account account2 = new Account("911", "李四", '女', "654321", 8000.0, 3000.0);
        accounts.add(account1);
        accounts.add(account2);
        while (true) {
            System.out.println("===欢迎您进入了黑马银行ATM系统===");
            System.out.println("1.用户登录");
            System.out.println("2.用户开户");
            System.out.println("请选择您要操作的命令：");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    System.out.println("==用户登录==");
                    login();
                    break;
                case 2:
                    System.out.println("==用户开户==");
                    createAccount();
                    break;
                default:
                    System.out.println("没有该操作~");
            }

            System.out.println("----------------------------------------");
        }
    }

    //完成用户的登录操作
    private void login() throws Exception {
        System.out.println("==系统登录==");
        if (accounts.size() == 0) {
            System.out.println("当前系统中无任何账户，请先开户再来登入");
            return;
        }
        while (true) {
            System.out.println("请您输入您的登录账号：");
            String cardId = sc.next();
            Account acc = getAccountByCardId(cardId);
            if (acc == null) {
                System.out.println("您输入的登录卡号不存在，请确认");
                throw new AccountNotFoundException("您输入的登录卡号不存在，请确认。。。");
            } else {
                while (true) {
                    System.out.println("请输入登录密码：");
                    String password = sc.next();
                    if (acc.getPassWord().equals(password)) {
                        loginAcc = acc;
                        System.out.println("恭喜您，" + acc.getUserName() + "成功登入了系统，您的卡号是：" + acc.getCardId());
                        showUserCommand();
                        return;
                    } else {
                        System.out.println("您输入的密码不正确，请确认");

                        throw new PasswordErrorException("密码输入错误！2！");

                    }

                }
            }

        }

    }

    //展示登录后的操作界面的
    private void showUserCommand() {
        while (true) {
            System.out.println(loginAcc.getUserName() + "您可以选择如下功能进行账户处理===");
            System.out.println("1、查询账户");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、转账");
            System.out.println("5、密码修改");
            System.out.println("6、退出");
            System.out.println("7、注销当前账户");
            System.out.println("请选择：");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    System.out.println("正在查询账户信息....");
                    showLoginAccount();//查询当前账户
                    break;
                case 2:
                    System.out.println("正在进行存款业务....");
                    depositMoney();//存款
                    break;
                case 3:
                    System.out.println("正在进行取钱业务....");
                    drawMoney();//取钱
                    break;
                case 4:
                    System.out.println("正在进行转账业务....");
                    transferMoney();//转账
                    break;
                case 5:
                    System.out.println("正在进行密码修改....");
                    updatePassWord();//密码修改
                    break;
                case 6:
                    //退出
                    System.out.println(loginAcc.getUserName() + "您退出系统成功！");
                    return;
                case 7:
                    if (deteAccount()) {//注销当前登录的账户
                        return;

                    }
                    break;
                default:
                    System.out.println("您当前的操作不存在的，请确认");
            }
        }
    }

    //账户密码修改
    private void updatePassWord() {
        System.out.println("==账户密码修改操作==");
        while (true) {
            System.out.println("请您输入当前账户的密码：");
            String password = sc.next();
            if (loginAcc.getPassWord().equals(password)) {
                while (true) {
                    System.out.println("请您输入新密码：");
                    String newPassWord = sc.next();
                    System.out.println("请您再次输入密码");
                    String okPassWord = sc.next();
                    if (okPassWord.equals(newPassWord)) {
                        loginAcc.setPassWord(okPassWord);
                        System.out.println("恭喜您，您的密码修改成功");
                        return;
                    } else {
                        System.out.println("您输入的2次密码不正确");
                    }
                }
            } else {
                System.out.println("您当前输入的密码不正确");
            }
        }
    }

    //销户操作
    private boolean deteAccount() {
        System.out.println("==进行销户操作==");
        System.out.println("请问您确认销户嘛？y/n");
        String command = sc.next();
        switch (command) {
            case "y":
                if (loginAcc.getMoney() == 0) {
                    accounts.remove(loginAcc);
                    System.out.println("您好，您的账户应经成功销户");
                    return true;


                } else {
                    System.out.println("对不起，您账户中存钱金额，不允许销户");
                    return false;
                }
            default:
                System.out.println("好的，您的账户保留！！");
                return false;
        }
    }

    //转账
    private void transferMoney() {
        System.out.println("==用户转账==");
        if (accounts.size() < 2) {
            System.out.println("当前系统中只有一个账户，无法为其他账户转账");
            return;
        }
        if (loginAcc.getMoney() == 0) {
            System.out.println("您自己都没钱了，就别转了");
            return;
        }
        while (true) {
            System.out.println("请您输入对方卡号：");
            String cardId = sc.next();

            Account acc = getAccountByCardId(cardId);
            if (acc == null) {
                System.out.println("您输入的对方卡号不存在");
            } else {
                String name = "*" + acc.getUserName().substring(1);
                System.out.println("请您输入【" + name + "】的姓氏：");
                String preName = sc.next();
                if (acc.getUserName().startsWith(preName)) {
                    while (true) {
                        System.out.println("请您输入转账给对方的金额：");
                        double money = sc.nextDouble();
                        if (loginAcc.getMoney() >= money) {
                            loginAcc.setMoney(loginAcc.getMoney() - money);
                            acc.setMoney(acc.getMoney() + money);
                            System.out.println("您转账成功了");
                            return;
                        } else {
                            System.out.println("您的余额不足，无法给对方转账这么多");
                        }

                    }

                } else {
                    System.out.println("对不起，你认证的姓氏有问题");
                }
            }
        }
    }

    //取钱
    private void drawMoney() {
        System.out.println("==取钱操作==");
        if (loginAcc.getMoney() < 100) {
            System.out.println("您的账户余额不足100元，不允许取钱");
            return;
        }
        while (true) {
            System.out.println("请输入取款金额：");
            double money = sc.nextDouble();

            if (loginAcc.getMoney() >= money) {
                if (money > loginAcc.getLimit()) {
                    System.out.println("您当前取款金额超过了每次限额，您每次最多可取：" + loginAcc.getMoney());

                } else {
                    loginAcc.setMoney(loginAcc.getMoney() - money);
                    System.out.println("您取款：" + money + "成功，取款后你剩余：" + loginAcc.getMoney());
                    break;
                }


            } else {
                System.out.println("余额不足，您账户中的余额是：" + loginAcc.getMoney());
            }
        }
    }

    //存钱
    private void depositMoney() {
        System.out.println("==存钱操作==");
        double money = 0;
        while (true) {
            System.out.println("请输入存款金额：");
            money = sc.nextDouble();
            if(money >0){
                break;
            }

            System.out.println("输入金额有误，请重新输入！！");
        }
        System.out.println("恭喜您，您存钱：" + money + "成功，存钱后余额是：" + loginAcc.getMoney());
        loginAcc.setMoney(loginAcc.getMoney() + money);
    }

    //展示当前登录的账户信息
    private void showLoginAccount() {
        System.out.println("==当前您的账户信息如下：==");
        System.out.println("卡号：" + loginAcc.getCardId());
        System.out.println("户主：" + loginAcc.getUserName());
        System.out.println("性别：" + loginAcc.getSex());
        System.out.println("余额：" + loginAcc.getMoney());
        System.out.println("每次取现额度：" + loginAcc.getLimit());
    }
    //完成用户开户操作
    private void createAccount() {
        System.out.println("==系统开户操作==");
        Account acc = new Account();
        System.out.println("请您输入您的账户名称：");
        String name = sc.next();
        acc.setUserName(name);
        while (true) {
            System.out.println("请您输入您的性别：");
            char sex = sc.next().charAt(0);
            if (sex == '男' || sex == '女') {
                acc.setSex(sex);
                break;
            } else {
                System.out.println("您输入的性别有误~只能是男或女~");
            }
        }
        while (true) {
            System.out.println("请您输入您的账户密码：");
            String password = sc.next();
            System.out.println("请您输入您的确认密码：");
            String okPassword = sc.next();
            if (okPassword.equals(password)) {
                acc.setPassWord(okPassword);
                break;
            } else {
                System.out.println("您输入的2次密码不一致，请您确认~~");
            }
        }
        while (true) {
            System.out.println("请您输入您的取现额度：");
            double limit = sc.nextDouble();
            if(limit>=100&&limit<=200){
                acc.setLimit(limit);
                break;
            }
            System.out.println("额度有误，重新输入！！");
        }

        String newCardId = createCardId();
        acc.setCardId(newCardId);

        accounts.add(acc);
        System.out.println("恭喜您，" + acc.getUserName() + "开户成功，您的卡号是：" + acc.getCardId());
    }

    private String createCardId() {
        while (true) {
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++) {
                int data = r.nextInt(10);
                cardId += data;
            }
            Account acc = getAccountByCardId(cardId);
            if (acc == null) {
                return cardId;

            }
        }
    }

    private Account getAccountByCardId(String cardId) {
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId)) {
                return acc;
            }
        }
        return null;
    }


}
